package br3akr.DeadlyDinnerParty.Server.Actions;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import br3akr.DeadlyDinnerParty.Server.Engine.Server;
import br3akr.DeadlyDinnerParty.Server.Engine.Session;

public class LoginAction extends Action implements MultiStepAction {
	private static final int START  = 1;
	private static final int VERIFY = 2;
	private static final int REALLY_VERIFY = 3;
	private int curStep;
	private ActionResponse actionResponse;
	private boolean actionComplete;
	private String playerName;
	
	public static final Set<String> keywords;
	static
	{
		Set<String>tempKeywords = new HashSet<String>();
		keywords = Collections.unmodifiableSet(tempKeywords);
	}
    
    public LoginAction() {
        super("Login", false);
		curStep = 1;
		actionComplete = false;
    }
    
    public ActionResponse doAction(String action, Session session) {
        actionResponse = new ActionResponse();
        actionResponse.setActingCharacter(session.getCharacter());
		
		switch (curStep) {
			case START:
                actionResponse.setActingCharacterDescription("Welcome to The Deadly Dinner Party!\n" +
                		"What is your real name?");
				curStep = VERIFY;
				break;
			case VERIFY:
				if (action != null && action.trim().length() != 0) {
					if (!Server.testing || (Server.testing && !Server.isTestName(action))) {
						actionResponse.setActingCharacterDescription("Your name is " + action + "?");
						playerName = action;
						curStep = REALLY_VERIFY;
					} else {
						actionComplete = true;
						session.setPlayerName(action);
						actionResponse.setActingCharacterDescription("Entering as test player: " + action);
						Server.world.addOnlinePlayer(session);
					}
				} else {
					actionResponse.setActingCharacterDescription("Please enter a valid name.");
					curStep = START;
				}
				break;
			case REALLY_VERIFY:
				if (action.equalsIgnoreCase("Y") || action.equalsIgnoreCase("Yes")) {
                    actionResponse.setActingCharacterDescription("Thanks, " + playerName + "!");
                    session.setPlayerName(playerName);
                    Server.world.addOnlinePlayer(session);
					actionComplete = true;
				} else {
                    actionResponse.setActingCharacterDescription("No? What is your name then?");
					curStep = VERIFY;
				}
				break;
			default:
				break;
		}
        
        return actionResponse;
    }
	
    public ActionResponse getActionResponse() {
		return actionResponse;
	}
    
    public Set<String> getKeywords() {
        return keywords;
    }

	@Override
	public boolean isComplete() {
		return this.actionComplete;
	}

	@Override
	public String getCurrentDescription() {
		// TODO Auto-generated method stub
		return null;
	}
}